package com.example.teaknow.Util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

/**
 * @Author: Stars
 */
public class DBHelper extends SQLiteOpenHelper {
    public static int DATABASE_VERSION = 1 ;
    public static final String DATABASE_NAME="Tea.db";//数据库名称
    public DBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE_NEWS="CREATE TABLE news (_id INTEGER PRIMARY KEY AUTOINCREMENT,title varchar(50),type varchar(20), time varchar(50), url TEXT, imageUrl TEXT,flag Integer)";
        String CREATE_TABLE_COLLECTION="CREATE TABLE if not exists collection (_id INTEGER PRIMARY KEY AUTOINCREMENT,title varchar(50),type varchar(20), time varchar(50), url TEXT, imageUrl TEXT)";
        String CREATE_TABLE_HISTORY="CREATE TABLE if not exists history (_id INTEGER PRIMARY KEY AUTOINCREMENT,title varchar(50),type varchar(20), time varchar(50), url TEXT, imageUrl TEXT,flag Integer)";
        String CREATE_TABLE_FEEDBACK="CREATE TABLE if not exists feedback (_id INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT)";
        db.execSQL(CREATE_TABLE_COLLECTION);
        db.execSQL(CREATE_TABLE_NEWS);
        db.execSQL(CREATE_TABLE_HISTORY);
        db.execSQL(CREATE_TABLE_FEEDBACK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //如果旧表存在，删除，所以数据将会消失
        db.execSQL("DROP TABLE IF EXISTS news");
        db.execSQL("DROP TABLE IF EXISTS collection");
        db.execSQL("DROP TABLE IF EXISTS history");
        db.execSQL("DROP TABLE IF EXISTS feedback");
        //再次创建表
        onCreate(db);
    }
}
